<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>查类是否已经定义</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.class-alias.html">« class_alias</a></li>
      <li style="float: right;"><a href="function.enum-exists.html">enum_exists »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.classobj.html">类/对象 函数</a></li>
    <li>查类是否已经定义</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.class-exists" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">class_exists</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">class_exists</span> &mdash; <span class="dc-title">查类是否已经定义</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.class-exists-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>class_exists</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$class</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$autoload</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   该函数检查指定的类是否已经定义。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.class-exists-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">class</code></dt>

     <dd>

      <p class="para">
       类名。名称以不区分大小写的方式匹配。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">autoload</code></dt>

     <dd>

      <p class="para">
       是否默认调用 <a href="language.oop5.autoload.html" class="link">__autoload</a>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.class-exists-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果 <code class="parameter">class</code> 是已经定义的类，则返回 <strong><code>true</code></strong>，否则返回 <strong><code>false</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.class-exists-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5022">
    <p><strong>示例 #1 <span class="function"><strong>class_exists()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;在尝试使用前检查类是否存在<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$myclass&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-5023">
    <p><strong>示例 #2 <code class="parameter">autoload</code> 参数示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />spl_autoload_register</span><span style="color: #007700">(function&nbsp;(</span><span style="color: #0000BB">$class_name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #0000BB">$class_name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;检查&nbsp;include&nbsp;后是否声明了类<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$class_name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">LogicException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;load&nbsp;class:&nbsp;</span><span style="color: #0000BB">$class_name</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />});<br /><br />if&nbsp;(</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">::class))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$myclass&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.class-exists-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.function-exists.html" class="function" rel="rdfs-seeAlso">function_exists()</a> - 如果给定的函数已经被定义就返回 true</span></li>
    <li class="member"><span class="function"><a href="function.enum-exists.html" class="function" rel="rdfs-seeAlso">enum_exists()</a> - 检测是否定义对应的枚举</span></li>
    <li class="member"><span class="function"><a href="function.interface-exists.html" class="function" rel="rdfs-seeAlso">interface_exists()</a> - 检查接口是否已被定义</span></li>
    <li class="member"><span class="function"><a href="function.get-declared-classes.html" class="function" rel="rdfs-seeAlso">get_declared_classes()</a> - 返回由已定义类的名字所组成的数组</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>